import {createSlice,createAsyncThunk} from "@reduxjs/toolkit";

import type {RootState} from "@/store";
import { getSongDetail, getSongLyric } from "../service";
import { ILyric, parseLyric } from "@/utils/parse-lyric";

interface IThunkState {
    state: RootState
}
export const fetchCurrentSongAction = createAsyncThunk<void,number,IThunkState>("currentSong",(id:number,{dispatch,getState})=>{

    const playSongList = getState().player.playSongList;
    const findIndex = playSongList.findIndex((item)=> item.id === id);
    if(findIndex === -1){ //未在歌单中找到歌曲
        getSongDetail(id).then((res)=>{
            // console.log(res);
            if(res.songs.length === 0) return;
            const song = res.songs[0];
            // 将song放到currentSong中
            const newPlaySongList = [...playSongList];
            newPlaySongList.push(song);
            dispatch(changeCurrentSongAction(song));
            dispatch(changePlaySongListAction(newPlaySongList));
            dispatch(changePlaySongIndexAction(newPlaySongList.length - 1));

        });
    }else{
        const song = playSongList[findIndex];
        dispatch(changeCurrentSongAction(song));
        dispatch(changePlaySongIndexAction(findIndex));
    };


    getSongLyric(id).then((res)=>{
        // console.log(res);
        const lyricString = res.lrc.lyric;
        const lyrics = parseLyric(lyricString);
        dispatch(changeLyricAction(lyrics));
    });
});
export const changeMusicAction = createAsyncThunk<void,boolean,IThunkState>("changemusic",(isNext,{dispatch,getState})=>{
    // 获取state中的数据
    const player = getState().player;
    const playMode = player.playMode;
    const songIndex = player.playSongIndex;
    const songList = player.playSongList;
    // 根据不同的模式计算不同的下一首歌曲的索引
    let newIndex = songIndex;
    if(playMode === 1){ //随机播放
        newIndex = Math.floor(Math.random() * songList.length);
    }else{ //单曲播放和顺序播放
        newIndex = isNext ? songIndex + 1 : songIndex - 1;
        if(newIndex > songList.length - 1) newIndex = 0;
        if(newIndex < 0) newIndex = songList.length - 1;
    }
    // 获取当前的歌曲
    const song = songList[newIndex];
    dispatch(changeCurrentSongAction(song));
    dispatch(changePlaySongIndexAction(newIndex));

    // 切换歌曲时重新获取最新歌词
    getSongLyric(song.id).then((res)=>{
        // console.log(res);
        const lyricString = res.lrc.lyric;
        const lyrics = parseLyric(lyricString);
        dispatch(changeLyricAction(lyrics));
    });
});

interface IPlayerState {
    currentSong:any
    playSongList:any[]
    playSongIndex:number
    playMode:number
    lyrics:ILyric[]
    lyricIndex:number
}

const initialState:IPlayerState = {
    currentSong:{},
    playSongList:[
        {
            name: '温柔',
            id: 386538,
            pst: 0,
            t: 0,
            ar: [
              {
                id: 13193,
                name: '五月天',
                tns: [],
                alias: []
              }
            ],
            alia: [],
            pop: 100,
            st: 0,
            rt: '600902000000534560',
            fee: 8,
            v: 76,
            crbt: null,
            cf: '',
            al: {
              id: 38285,
              name: '我们是五月天',
              picUrl:
                'https://p2.music.126.net/v4V40sXKnaqsG0ACyY0aDg==/109951164912221924.jpg',
              tns: [],
              pic_str: '109951164912221924',
              pic: 109951164912221920
            },
            dt: 269800,
            h: {
              br: 320000,
              fid: 0,
              size: 10794885,
              vd: -63963,
              sr: 44100
            },
            m: {
              br: 192000,
              fid: 0,
              size: 6476948,
              vd: -61380,
              sr: 44100
            },
            l: {
              br: 128000,
              fid: 0,
              size: 4317980,
              vd: -59700,
              sr: 44100
            },
            sq: {
              br: 1053723,
              fid: 0,
              size: 35536822,
              vd: -63997,
              sr: 44100
            },
            hr: null,
            a: null,
            cd: '1',
            no: 2,
            rtUrl: null,
            ftype: 0,
            rtUrls: [],
            djId: 0,
            copyright: 0,
            s_id: 0,
            mark: 8704,
            originCoverType: 1,
            originSongSimpleData: null,
            tagPicList: null,
            resourceState: true,
            version: 76,
            songJumpInfo: null,
            entertainmentTags: null,
            awardTags: null,
            single: 0,
            noCopyrightRcmd: null,
            rurl: null,
            rtype: 0,
            mst: 9,
            cp: 684010,
            mv: 10929721,
            publishTime: 1049126400000
        },
        {
            name: '起风了',
            id: 1330348068,
            pst: 0,
            t: 0,
            ar: [
              {
                id: 12085562,
                name: '买辣椒也用券',
                tns: [],
                alias: []
              }
            ],
            alia: [],
            pop: 100,
            st: 0,
            rt: '',
            fee: 8,
            v: 42,
            crbt: null,
            cf: '',
            al: {
              id: 74715426,
              name: '起风了',
              picUrl:
                'https://p1.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg',
              tns: [],
              pic_str: '109951163699673355',
              pic: 109951163699673360
            },
            dt: 325868,
            h: {
              br: 320000,
              fid: 0,
              size: 13037236,
              vd: -77525,
              sr: 44100
            },
            m: {
              br: 192000,
              fid: 0,
              size: 7822359,
              vd: -74987,
              sr: 44100
            },
            l: {
              br: 128000,
              fid: 0,
              size: 5214920,
              vd: -73504,
              sr: 44100
            },
            sq: {
              br: 985873,
              fid: 0,
              size: 40158105,
              vd: -77524,
              sr: 44100
            },
            hr: {
              br: 2832349,
              fid: 0,
              size: 115371553,
              vd: -77475,
              sr: 88200
            },
            a: null,
            cd: '1',
            no: 1,
            rtUrl: null,
            ftype: 0,
            rtUrls: [],
            djId: 0,
            copyright: 0,
            s_id: 0,
            mark: 536879104,
            originCoverType: 1,
            originSongSimpleData: null,
            tagPicList: null,
            resourceState: true,
            version: 42,
            songJumpInfo: null,
            entertainmentTags: null,
            awardTags: null,
            single: 0,
            noCopyrightRcmd: null,
            mv: 10782615,
            rtype: 0,
            rurl: null,
            mst: 9,
            cp: 1415923,
            publishTime: 0
        }
    ],
    playSongIndex:-1,
    playMode:0, //0：顺序播放 1：随机播放 2：单曲循环
    lyrics:[],
    lyricIndex:-1
};

const playerSlice = createSlice({
    name:"player",
    initialState,
    reducers:{
        changeCurrentSongAction(state,{payload}){
            state.currentSong = payload;
        },
        changeLyricAction(state,{payload}){
            state.lyrics = payload;
        },
        changeLyricIndexAction(state,{payload}){
            state.lyricIndex = payload;
        },
        changePlaySongIndexAction(state,{payload}){
            state.playSongIndex = payload;
        },
        changePlaySongListAction(state,{payload}){
            state.playSongList = payload;
        },
        changePlayModeAction(state,{payload}){
            state.playMode = payload;
        }
    }
});

export const {changeCurrentSongAction,
    changePlaySongIndexAction,
    changeLyricAction,
    changeLyricIndexAction,
    changePlaySongListAction,
    changePlayModeAction} = playerSlice.actions;
export default playerSlice.reducer;
